from app.web.comm.test_data import TestData
import paramunittest
from app.web.data.data_config import Config
import unittest
from app.web.comm.operate_json import OperationJson
from app.web.comm.test_method import RunMain
from jsonpath_rw import jsonpath ,parse
from app.web.comm.operate_excel import  OperateExcel
Te = TestData()

class GetData(unittest.TestCase):
    def __init__(self):
        self.Opera_json = OperationJson()
        self.Opera_excel = OperateExcel()
        self.Run = RunMain()
        self.config = Config()
        # self.opera_excel=OperateExcel()

#根据data关键字获取json格式的请求数据
    def get_data_for_json(self, json_id):
        self.Opera_json.data_new()
        request_data = self.Opera_json.get_data(file_path='../data/request_data.json',id=json_id )
        return request_data

#根据行号获取json格式的请求数据
    def get_json_data(self,row):
        request_data=self.Opera_json.get_data(file_path='../data/request_data.json',id=self.get_request_data(row))
        return request_data

#获取请求数据data关键字
    def get_request_data(self,row):
        col=int(self.config.get_conf()['data'])
        request_data=self.Opera_excel.get_cell_value(row,col)
        return request_data

#判断依赖值（depend_data）是否为空,不为空则返回值，为后续是否执行depend_case,及行号提供依据
    def get_depend_caseid(self):
        depend_caseid=self.depend_case
        if depend_caseid == '':
            return None
        else:
            return depend_caseid

# 获取依赖数据的key返回值
    def get_depend_data(self, row):
        col = int(self.config.get_conf()['depend_data'])
        depent_data = self.Opera_excel.get_cell_value(row, col)
        if depent_data == "":
            return None
        else:
            return depent_data

#执行被依赖case，获取依赖case需要的依赖值
    def get_depend_data(self, t_data, depent_data,d_caseid):#后面通过input_value,self.depend_data,self.depend_case传参
        d_case_id = int(d_caseid)
        d_json_id=t_data[d_case_id]['data']
        # request_data=t_data[d_case_id]['data']
        case_name=t_data[d_case_id]['case_name']
        request_url=t_data[d_case_id]['url']
        request_method=t_data[d_case_id]['method']
        request_header=t_data[d_case_id]['is_header']
        request_cookie=t_data[d_case_id]['cookie']
        request_data=self.get_data_for_json(json_id=d_json_id) #获取依赖case的请求数据
        res = self.Run.run_main(url=request_url, method=request_method, data=request_data,
                              header=request_header, cookie=request_cookie)
        json_exe = parse(depent_data)
        #以上为被依赖case数据
        print("case_name=%s"%case_name)
        print("request_data=%s"%request_data)
        print ("res=%s"%res)
        json_exe = parse(depent_data)
        print("json_exe:%s"%json_exe)
        madle = json_exe.find(res)#依赖case的返回结果
        print("madle=%s"%madle)
        return [math.value for math in madle][0]  # 固定格式

#写入测试结果
    # def write_result(self, row, value):
    #     # col=int(self.config.get_conf()['result'])
    #     # self.opera_excel.write_data(row,col,value)

if __name__=='__main__':
    G=GetData()
    # G.get_data_for_json('token')
    t_data=Te.excel_table_byindex(file='../data/Interface_data1.xls', colnameindex=0)
    depent_data='data.token'
    d_caseid=1
    G.get_depend_data(t_data,depent_data,d_caseid)





